/*
 * dbg_display.h
 *
 *  Created on: 2021/8/25
 *      Author: Yaochaowei
 *       Email: 545200059@qq.com
 *
 *       Brief: Redirect DBG_INFO() used by Display module or virtual UART module
 */

/** How to use TI's display module :
 *      1. enable display module in .syscfg file;
 *      2. add define "DBG_DISPLAY" in project's properties;
 *      1. add following code in main_tirtos.c:
 *
            #include <ti/display/Display.h>

            #if defined(DBG_DISPLAY)
            Display_Handle        dispHandle;
            void APP_openDisplay(void)
            {
                Display_Params dispParams;
                Display_Params_init(&dispParams);

                dispHandle = Display_open(Display_Type_UART, &dispParams);
                if(dispHandle == NULL)
                {

                }
            }
            #endif
 *
 *      2. add following code in empty.cpp:
 *
            #include "dbger.h"

            extern "C" {
            #if defined(DBG_DISPLAY)
            extern void APP_openDisplay(void);
            #endif
            void *mainThread(void *arg0);
            }

 *      3. call DBG_INIT() in function mainThread() in empty.cpp, then can use DBG_INFO() to print LOG;
 *
 */

#ifndef DBGER_H
#define DBGER_H

/* ---------------------------------------------------------------------------
 *                              Includes
 * ---------------------------------------------------------------------------
 */
#include <stddef.h>
#include <stdint.h>
//#include "SEGGER_RTT.h"
#ifdef __cplusplus
extern "C" {
#endif

/* ---------------------------------------------------------------------------
 *                              Global Macros
 * ---------------------------------------------------------------------------
 */
#ifdef DBG_COLOR
    #define TEXT_RED       "\x1B[2;31m"
    #define TEXT_YELLOW    "\x1B[2;33m"
    #define TEXT_GREEN     "\x1B[2;32m"
    #define TEXT_RESET     "\x1B[0m"
#else
    #define TEXT_RED
    #define TEXT_YELLOW
    #define TEXT_GREEN
    #define TEXT_RESET
#endif

#if defined(DBG_DISPLAY)        /* if use display module */
    #include <ti/display/Display.h>
    #define DBG_INIT()               APP_openDisplay() /*implement in main.c */
    #define DBG_INFO(fmt, ...)       Display_printf(dispHandle, 0, 0, TEXT_RESET fmt, ##__VA_ARGS__)
    #define DBG_DBG(fmt, ...)        Display_printf(dispHandle, 0, 0, TEXT_GREEN fmt TEXT_RESET, ##__VA_ARGS__)
    #define DBG_STAT(fmt, ...)       Display_printf(dispHandle, 0, 0, TEXT_GREEN fmt TEXT_RESET, ##__VA_ARGS__)
    #define DBG_WARRING(fmt, ...)    Display_printf(dispHandle, 0, 0, TEXT_YELLOW fmt TEXT_RESET, ##__VA_ARGS__)
    #define DBG_ERROR(fmt, ...)      Display_printf(dispHandle, 0, 0, TEXT_RED fmt TEXT_RESET, ##__VA_ARGS__)
    #define DBG_FATAL(fmt, ...)      Display_printf(dispHandle, 0, 0, TEXT_RED "%s" fmt TEXT_RESET, __FUNCTION__, ##__VA_ARGS__)
        #ifndef  DISP_MAIN      /* avoid extern declaraion in main.c */
        extern Display_Handle        dispHandle;
        #endif
#elif defined(DBG_VIRTUAL_UART)  /* if use virtual uart module */
    #include "virtualUart.h"
    #define DBG_INIT()               Virtual_UART_init()
    #define DBG_INFO(fmt, ...)       Virtual_UART_printf(VIRTUAL_UART_0, TEXT_RESET fmt, ##__VA_ARGS__)
    #define DBG_DBG(fmt, ...)        Virtual_UART_printf(VIRTUAL_UART_0, TEXT_GREEN fmt TEXT_RESET, ##__VA_ARGS__)
    #define DBG_STAT(fmt, ...)       Virtual_UART_printf(VIRTUAL_UART_1, TEXT_GREEN fmt TEXT_RESET, ##__VA_ARGS__)
    #define DBG_WARRING(fmt, ...)    Virtual_UART_printf(VIRTUAL_UART_0, TEXT_YELLOW fmt TEXT_RESET, ##__VA_ARGS__)
    #define DBG_ERROR(fmt, ...)      Virtual_UART_printf(VIRTUAL_UART_0, TEXT_RED fmt TEXT_RESET, ##__VA_ARGS__)
    #define DBG_FATAL(fmt, ...)      Virtual_UART_printf(VIRTUAL_UART_0, TEXT_RED "%s" fmt TEXT_RESET, __FUNCTION__, ##__VA_ARGS__)
    //#define DBG_FATAL(fmt, ...)      Virtual_UART_printf(VIRTUAL_UART_0, TEXT_RED "[%s]%s line: %d" fmt TEXT_RESET, __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__)
#elif defined(DBG_RTT)
    #include "SEGGER_RTT.h"
    #include "SEGGER_RTT_Conf.h"
    #define DBG_INIT()               SEGGER_RTT_Init()
    #define DBG_T0(id, fmt, ...)     SEGGER_RTT_printf(id, fmt "\r\n", ##__VA_ARGS__);
    #define DBG_T1(id, fmt, ...)     do { \
                                         SEGGER_RTT_SetTerminal(1);\
                                         SEGGER_RTT_printf(id, fmt "\r\n", ##__VA_ARGS__);\
                                         SEGGER_RTT_SetTerminal(0);\
                                     } while(0)
    #define DBG_INFO(fmt, ...)       DBG_T0(0, TEXT_RESET fmt, ##__VA_ARGS__)
    #define DBG_DBG(fmt, ...)        DBG_T0(0, TEXT_GREEN fmt TEXT_RESET, ##__VA_ARGS__)
    #define DBG_STAT(fmt, ...)       DBG_T1(0, TEXT_GREEN fmt TEXT_RESET, ##__VA_ARGS__)
    #define DBG_WARRING(fmt, ...)    DBG_T0(0, TEXT_YELLOW fmt TEXT_RESET, ##__VA_ARGS__)
    #define DBG_ERROR(fmt, ...)      DBG_T0(0, TEXT_RED fmt TEXT_RESET, ##__VA_ARGS__)
    #define DBG_FATAL(fmt, ...)      DBG_T0(0, TEXT_RED "%s" fmt TEXT_RESET, __FUNCTION__, ##__VA_ARGS__)
#else /* close dbg log */
    #define DBG_INIT()
    #define DBG_INFO(fmt, ...)
    #define DBG_DBG(fmt, ...)
    #define DBG_STAT(fmt, ...)
    #define DBG_WARRING(fmt, ...)
    #define DBG_ERROR(fmt, ...)
    #define DBG_FATAL(fmt, ...)
#endif

#ifdef __cplusplus
}
#endif

#endif /* DBGER_H */
